package jjn.round1;

import java.util.Objects;

/**
 * @author Jjn
 * @since 2022/9/6 22:40
 */
public class LeetCode844_BackspaceStringCompare {
    public boolean backspaceCompare(String s, String t) {
        String sLength = backspace(s);
        String tLength = backspace(t);
        return Objects.equals(sLength, tLength);
    }

    private String backspace(String s) {
        int length = s.length();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == '#') {
                if (stringBuilder.length() > 0) {
                    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                }
                continue;
            }
            stringBuilder.append(c);
        }
        return stringBuilder.toString();
    }
}
